﻿using LeHouse.Server.Dto;
using LeHouse.Server.Service;
using LeHouse.Server.WebApi.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace LeHouse.Server.WebApi.Controllers
{
    public class DictionaryController : ApiController
    {
        //字典表的实例化
        System_DictionaryService dic = new System_DictionaryService();

        /// <summary>
        /// 获取所有枚举定义的字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Dictionary/GetSelectType")]
        public dynamic GetSelectType()
        {
            var dropDic = EnumHelperDic.EnumListDic<EnumSelectType>(0, "--请选择字典类型--");
            return Json(dropDic);
        }

        /// <summary>
        /// 通用下拉框的获取
        /// </summary>
        /// <param name="Dmenuid">枚举编号</param>
        /// <returns>符合要求的下拉框数据</returns>
        [HttpGet]
        [Route("api/Dictionary/GetDictionaryList")]
        public List<List<System_Dictionary>> GetDictionaryList([FromUri]int[] Dmenuid)
        {
            List<List<System_Dictionary>> ls = new List<List<System_Dictionary>>();
            for (int i = 0; i < Dmenuid.Length; i++)
            {
                List<System_Dictionary> droplist = dic.Select(p => p.Dmenuid == Dmenuid[i]);
                ls.Add(droplist);
            }
            return ls;
        }

        /// <summary>
        /// 通用下拉框的获取
        /// </summary>
        /// <param name="Dmenuid">枚举编号</param>
        /// <returns>符合要求的下拉框数据</returns>
        [Route("api/Dictionary/GetDictionary")]
        public IEnumerable<System_Dictionary> GetDictionary([FromUri]int Dmenuid)
        {
            //字典表的实例化
            List<System_Dictionary> droplist = dic.Select(p => p.Dmenuid == Dmenuid);
            return droplist;
        }

        /// <summary>
        /// 显示字典表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Dictionary/Select")]
        public List<dynamic> showdictionary([FromBody] int Dmenuid)
        {
            System_DictionaryService showdic = new System_DictionaryService();
            List<System_Dictionary> lsit = new List<System_Dictionary>();
            if (Dmenuid == 0)
            {
                lsit = showdic.Select();
            }

            lsit = showdic.Select().Where(p => p.Dmenuid == Dmenuid).ToList();

            return (from v in lsit
                    select new { v.DictionaryID, v.DictionaryName, DmenuidName = EnumHelperDic.GetEnumSelectState<EnumSelectType>(v.Dmenuid) }).ToList<dynamic>();

        }

        /// <summary>
        /// 添加字典表信息
        /// </summary>
        /// <param name="dic">ajax传过来的要添加的数据</param>
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Dictionary/Postinsert")]
        public bool Insert([FromBody] System_Dictionary dic)
        {
            System_DictionaryService insert = new System_DictionaryService();
            var d1 = insert.getDictionarysByDmenuid(dic.Dmenuid);//根据类型分组id获取属于该类型的字典信息
            if (d1 == null)
            {
                dic.DictionaryID = dic.Dmenuid + 0001;
            }
            else
            {
                try
                {
                    int DictionaryID = 0;//Convert.ToInt32(d1.T_InquiryRecordID) + 1;
                    dic.DictionaryID = DictionaryID;
                }
                catch
                {
                    return false;
                }
            }

            if (insert.Insert(dic) == true)//添加字典信息，成功返回true；
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除字典表信息
        /// </summary>
        /// <param name="id">获取要删除的字典表信息</param>
        /// 
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Dictionary/Delete")]
        public bool Delete([FromBody] int id)
        {
            System_DictionaryService delete = new System_DictionaryService();
            if (delete.DeleteById(id) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        ///根据编号获取要修改的字典表信息
        /// </summary>
        /// <param name="id">获取原有的信息</param>
        /// <returns>false/true</returns>
        [HttpPost]
        [Route("api/Dictionary/UpdateByid")]
        public System_Dictionary UpdateByid([FromBody]int id)
        {
            System_DictionaryService updatebyid = new System_DictionaryService();
            var d1 = updatebyid.SelectById(id);
            return d1;
        }
        /// <summary>
        /// 修改字典表信息
        /// </summary>
        /// <param name="dic">获取已修改的信息然后传回去</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Dictionary/Update")]
        public bool Update([FromBody]System_Dictionary dic)
        {
            System_DictionaryService update = new System_DictionaryService();
            if (update.Update(dic) == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
